#include "pch.h"
#include "editBox.h"
namespace mini
{
	namespace ui
	{
		CEditBox::CEditBox()
		{
			mWndType = ui_editBox;
		}
		CEditBox::CEditBox(const std::string text) : CEditBox()
		{
			setWndName(text);
		}

		CEditBox::~CEditBox()
		{

		}

		void CEditBox::setCursorIndex(int index)
		{
			size_t l = mWindowText.length();
			if (l > 0 && index <= l )
			{
				mInsertIndex = index;
			}
			else
			{
				mInsertIndex = 0;
			}
		}

		int CEditBox::getCursorIndex()
		{
			return mInsertIndex;
		}

		void CEditBox::inputCharacter(UINT16 nChar)
		{
			mWindowText.insert(mInsertIndex, 1, nChar);
			++mInsertIndex;
			_checkCursorPlace();

		}

		void CEditBox::keyboardEvent(UINT16 nKey)
		{
			switch (nKey)
			{
			case VK_LEFT:
				mInsertIndex -= 1;
				break;
			case VK_RIGHT:
				mInsertIndex += 1;
				break;
			case VK_DELETE:
				if (mInsertIndex < mWindowText.length())
				{
					mWindowText.erase(mInsertIndex);
				}
				break;
			}
			_checkCursorPlace();

		}

		void CEditBox::_checkCursorPlace()
		{
			if (mInsertIndex < 0)
			{
				mInsertIndex = 0;
			}
			else if (mInsertIndex > mWindowText.length())
			{
				--mInsertIndex;
			}
		}

	}
}